import dataUtil from "../framework/modules/dataUtil";
import { httpUtil } from "../framework/modules/httpUtil";
import Common from "../utils/Common";
import { Debug } from "../utils/Debug";
import OPPOModule from "./OPPOModule";
import { platformConf } from "./platformConf";
import { platformUtil } from "./platformUtil";

export default class ZSOPPOModule extends OPPOModule {



    /**
    * 检查当前版本的导出广告是否开启
    * @param {string} version 
    * @param {*} callback 
    * @returns callback回调函数的参数为boolean，true：打开广告，false：关闭广告
    */
    checkVersion(version: string, callback: (openAd: boolean) => void) {
        httpUtil.loadCfg(res => {
            let openAd = super.checkLog(res.zs_version)
            callback(openAd)
        })

    }



    login(success?: Function, fail?: Function) {
        if (!this.checkWindow()) {
            if (success)
                success()
            return;
        }
        var win = this.getWindow();
        win[this.platformName].login({
            success: function (res: any) {
                // var data = JSON.stringify(res.data);
                // Debug.log(res.data.token);
                var url = "https://platform.qwpo2018.com/api/oppo_login/index";
                httpUtil.request(url, {
                    apk_id: platformConf.config.appId,
                    code: res.data.token
                }, 'POST', (res2: any) => {

                    dataUtil.setToken(res2.data.user_id)
                    if (success)
                        success(res2.data)
                    Debug.log('platformLogin success ', res2)
                }, (res2: any) => {
                    if (success)
                        success(null);
                    Debug.log('platformLogin fail ', res2)
                })
            },
            fail: function (res: any) {
                if (fail)
                    fail(res)
            }
        });

    }

    private navigateCallback(appId: string) {

        let url = 'https://platform.qwpo2018.com/api/apk_ad/click_log';
        let openId = dataUtil.getToken();
        var signParams = {
            user_id: openId,
            apk_id: platformConf.config.appId,
            appid: appId,
            link_id: appId,
        };
        let data = signParams;
        Debug.log('跳转数据上报', data)
        httpUtil.request(url, data, 'POST',
            (res: any) => {
                Debug.log('跳转数据上报成功', res)
            },
            (res: any) => {
                Debug.log('跳转数据上报失败', res)
            },
            () => {
                Debug.log('upload navigate complete');
            });
    }

}